package com.ewanghuiju.app.ui.web;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.gyf.immersionbar.ImmersionBar;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class webview
 * @time 2020/2/11
 */
@SuppressLint("SetJavaScriptEnabled")
public class MyWebviewActivity extends WebViewActivity {
    Activity activity;
    public static final String SURL = "https://www.hao123.com/";
    ImageView iv_refresh;
    //    LinearLayout iv_xxclose;
    LinearLayout titleBack;
    @BindView(R.id.layout_bar)
    RelativeLayout layoutBar;
    private TextView bar_title;

    private String webTitle;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        JsUtils.onActivityResult(activity, requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onChangeTitle(String title) {
        super.onChangeTitle(title);
        if (TextUtils.isEmpty(webTitle)) {
            setBarTitle(title);
        }
    }

    public void setBarRight(String str) {
        //if (SampleApplicationLike.IS_DEBUG){
//        bar_right.setText(str);
//        bar_right.setVisibility(View.VISIBLE);
        // }

    }

    @Override
    public void initData() {
//        JsUtils.register(activity,x5WebViewClient,new JsUtils.OnListener(){
//
//            @Override
//            public void onTest(JsPromptResult result, int type, String json) {
////                Toasts.show(activity,json);
////                JsUtils.send( result,new JsDataBean(type,"我是安卓，js传过来的参数是"+json+",调用安卓成功"));
//            }
//            @Override
//            public void onWxPay(JsPromptResult result,int type,String json) {
////                resultPay = result;
////                PayUtils.request(activity,1,json);
//            }
//
//
//            @Override
//            public void onAliPay(JsPromptResult result,int type,String json) {
////                resultPay = result;
////                PayUtils.request(activity,2,json);
//            }
//
//
//
//            @Override
//            public void onDestoryActivity(JsPromptResult result, int type, String json) {
////                finish();
//            }
//        });

        String url = getIntent().getStringExtra("url");
        if (TextUtils.isEmpty(url)) {
            url = SURL;
        }
        loadUrl(url);
    }

    @Override
    public void pageFinish() {
        //QTLog.e("pageFinish");
//        if (canBack(false)){
//            iv_xxclose.setVisibility(View.VISIBLE);
//        }else {
//            iv_xxclose.setVisibility(View.GONE);
//        }
    }

    public void setBarTitle(String str) {
        bar_title.setText(str);
    }


    public void onFinish(View view) {
        super.onBackPressed();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_my_webview;
    }

    @Override
    protected int getViewId() {
        return R.id.x5webViewClients;
    }

    @Override
    protected void initView() {
        initActionBar();
        webTitle = getIntent().getStringExtra(Constants.WEBURL_TITLE);
        if (!TextUtils.isEmpty(webTitle)) {
            bar_title.setText(webTitle);
        }
    }

    private void initActionBar() {
        titleBack = findViewById(R.id.titleBack);
        bar_title = findViewById(R.id.bar_title);
    }

    @OnClick({R.id.titleBack})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.titleBack:
                onBackPressedSupport();
                break;
            default:
                break;
        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(layoutBar, true)
                .statusBarDarkFont(true)
                .init();
    }
}